//
//  SearchView.m
//  Tunnel
//
//  Created by wbb on 2025/6/29.
//

#import "SearchView.h"
@interface SearchView ()<UITextFieldDelegate>
@property (nonatomic, copy) NSString * searchStr;
@end
@implementation SearchView

- (instancetype)initWithTitle:(NSString *)title {
    self = [super init];
    if(self) {
        
        UIButton *backBtn = [UIButton buttonWithImageName:@"back_image" selectImageName:@"" target:self action:@selector(goBack)];
        [self addSubview:backBtn];
        [backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.offset(20);
            make.centerY.offset(0);
        }];
        
        UIButton *searchBtn = [UIButton buttonWithTitle:@"搜索" titleColor:[UIColor whiteColor] font:[UIFont systemFontOfSize:14] target:self action:@selector(goSearch)];
        searchBtn.backgroundColor = main_color;
        searchBtn.cornerRadius = 2;
        [self addSubview:searchBtn];
        [searchBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.offset(-12);
            make.centerY.offset(0);
            make.width.offset(50);
            make.height.offset(30);
        }];
        
        UIView *tfView = [[UIView alloc] init];
        tfView.backgroundColor = hexColor(dddddd);
        tfView.cornerRadius = 2;
        [self addSubview:tfView];
        [tfView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(searchBtn.mas_left).offset(-8);
            make.left.equalTo(backBtn.mas_right).offset(0);
            make.centerY.offset(0);
            make.height.offset(30);
        }];
        
        UIImageView *searchView = [[UIImageView alloc] init];
        searchView.image = [UIImage imageNamed:@"nav_search"];
        [tfView addSubview:searchView];
        [searchView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.offset(5);
            make.centerY.offset(0);
            make.width.height.offset(30);
        }];
        
        UITextField *tf = [[UITextField alloc] init];
        tf.placeholder = [NSString stringWithFormat:@"请输入%@", title];
        tf.font = [UIFont systemFontOfSize:14];
        tf.returnKeyType = UIReturnKeySearch; // 设置键盘返回键为搜索按钮
            tf.delegate = self; // 设置代理
        [tf addTarget:self action:@selector(textChange:) forControlEvents:UIControlEventEditingChanged];
        [tfView addSubview:tf];
        [tf mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(searchView.mas_right).offset(5);
            make.right.offset(-12);
            make.centerY.offset(0);
        }];
        
        [tf becomeFirstResponder];
    }
    return self;
}

- (void)textChange:(UITextField *)tf {
    self.searchStr = tf.text;
}

// 点击键盘上的搜索按钮时触发
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder]; // 收起键盘
    
    if(self.searchBlock){
        self.searchBlock(self.searchStr);
    }
    return YES;
}
- (void)goSearch {
    if(self.searchBlock){
        self.searchBlock(self.searchStr);
    }
}
- (void)goBack {
    [WDGlobal shareInstance].isNeedSarch = NO;
    [[WDGlobal getCurrentViewController] dismissViewControllerAnimated:YES completion:nil];
}
// 执行搜索逻辑
//- (void)performSearchWithText:(NSString *)searchText {
//    if (searchText.length == 0) {
//        self.filteredData = self.dataSource; // 搜索文本为空时显示全部数据
//    } else {
//        // 使用 NSPredicate 过滤数据
//        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[c] %@", searchText];
//        self.filteredData = [self.dataSource filteredArrayUsingPredicate:predicate];
//    }
//    
//    [self.tableView reloadData]; // 刷新表格显示搜索结果
//}
@end
